﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BarberShop.BLL;
using BarberShop.Data;
using Maticsoft.Common;
using System.Drawing;
using System.Web.UI.HtmlControls;
using System.IO;
using BarberShop.Common.Enum;


public partial class BarberAdd : BasePage
{
    public int Id
    {
        get
        {
            return string.IsNullOrEmpty(Request.QueryString["id"]) ? 0 : Convert.ToInt32(Request.QueryString["id"]);
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetStores();
            GetBarberType();
            GetData();
            
        }
    }


    protected void GetData()
    {
        if (Id != 0)
        {
            var info = BarberInfoBLL.GetBarberInfo(Id);
            txt_Name.Text = info.Name;
            txt_desc.Text = info.Description;
        }
    }
    //绑定理发师类别
    protected void GetBarberType()
    {
        this.DropDownList1.DataSource = BarberTypeBLL.GetBarberTypeList("");
        this.DropDownList1.DataTextField = "Name";
        this.DropDownList1.DataValueField = "ID";
        this.DataBind();
    }
    //绑定门店
    protected void GetStores()
    {
        this.drp_store.DataSource = StoresInfoBLL.GetStoresList(new BarberShop.Model.StoresInfoSearch());
        this.drp_store.DataTextField = "StoreName";
        this.drp_store.DataValueField = "ID";
        this.drp_store.DataBind();
    }
   
    //添加
    protected void Button1_Click(object sender, EventArgs e)
    {

        string AdImgUrl = ConfigHelper.GetConfigString("UpFileUrl") + "Shop_" + drp_store.SelectedValue.Trim() + "\\barber\\";
        string bigimg = "";
        string smaillimg = "";
        string adname = txt_Name.Text.Trim();
        string r = "";
        if (File1.Value != "")
        {
            r = UpLoadImage(File1, AdImgUrl, "_Thumb", 120, 120, out bigimg, out smaillimg);
        }
        if (r == "")
        {
            if (Id == 0)
            {
                BarberInfo info = new BarberInfo();
                info.Name = adname;
                info.Bigimgname = bigimg;
                info.Smallimgname = smaillimg;
                info.Type = int.Parse(DropDownList1.SelectedValue);
                info.Stores_ID = int.Parse(drp_store.SelectedValue);
                info.Description = txt_desc.Text.Trim();
                info.CreateDate = DateTime.Now;
                bool rr=BarberInfoBLL.AddBarberInfo(info);
                if (rr)
                { 
                    UpdateFiles fileinfo=new UpdateFiles();
                    //fileinfo.LinkPath = UpdateFileUrl+"keyner/shop/barber/" + info.Bigimgname;
                    fileinfo.LinkPath = UpdateFileUrl + "Notice/DownloadFiles.ashx?Type=" + ((int)TableType.BarberInfoTable) + "&StoreId=" + drp_store.SelectedValue.Trim() +"&FileId="+info.ID+ "&fileName=" + info.Bigimgname;
                    fileinfo.Status = (int)FileStatus.Normal;
                    fileinfo.Stores_ID = info.Stores_ID;
                    fileinfo.TableID = (int)TableType.BarberInfoTable;
                    fileinfo.FileID = info.ID;
                    fileinfo.CreateDate = DateTime.Now;
                    fileinfo.FileName = info.Bigimgname;
                    fileinfo.FilePath = "keyner/shop/barber/";
                    UpdateFilesBLL.Add(fileinfo);
                }
            }
            else
            {
                BarberInfo info = BarberInfoBLL.GetBarberInfo(Id);
                info.Name = adname;
                info.Stores_ID = int.Parse(drp_store.SelectedValue);
                info.Type = int.Parse(DropDownList1.SelectedValue);
                info.Description = txt_desc.Text.Trim();
                if (File1.Value != "")
                {
                    info.Bigimgname = bigimg;
                    info.Smallimgname = smaillimg;
                    UpdateFilesBLL.UpdateFileName(TableType.BarberInfoTable, info.ID, bigimg);
                }
                BarberInfoBLL.UpdateBarberInfo(info);
            }
            MessageBox.Show(this.Page, "保存理发师成功！");

        }
        else
        {
            MessageBox.Show(this.Page, r);
        }
    }
   
}
